package com.kgc.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.entity.User;
import com.kgc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController1 {
    @Autowired
    private UserService userService;

    @RequestMapping("pageList")
    public ModelAndView list(
            @RequestParam(value = "page",required = false,defaultValue = "1")int page
            , @RequestParam(value = "pageSize",required = false,defaultValue = "5")  int pageSize,
            User user) {
        //封装转跳转的视图，充当作用对象
        ModelAndView mav=new ModelAndView();
        PageInfo pageInfo=userService.findByPage(page,pageSize,user);
        mav.addObject("pageInfo",pageInfo);
        mav.addObject("user",user);
        mav.setViewName("list");
        return mav;
    }

    @RequestMapping("delete/{id}")
    public String deleteUser(@PathVariable("id")int id){
        userService.delete(id);
        return "redirect:/user/pageList";
    }

    @RequestMapping("deletes")
    public String deletes(int[] ids){
        System.out.println(Arrays.toString(ids));
        userService.deleteByIds(ids);
        return "redirect:/user/pageList";
    }
}
